package com.xj.mydemo.Activity;


import android.content.Intent;
import android.os.Bundle;
import android.support.v7.widget.Toolbar;
import android.util.Log;
import android.view.View;
import android.widget.Button;
import android.widget.TextView;
import android.widget.Toast;

import com.xj.mydemo.Base.BaseActivity;
import com.xj.mydemo.Entity.UpdateInfo;
import com.xj.mydemo.HttpInterface.UpdateService;
import com.xj.mydemo.R;

import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

public class UpdateActivity extends BaseActivity implements View.OnClickListener {

    private final static String TAG = "UpdateActivity";

    private TextView resultTV;
    private Button btnUpdate;
    private Toolbar mToolbar;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_update);
        initView();
        initData();
    }

    private void initData() {
    }

    private void initView() {
        resultTV = (TextView) findViewById(R.id.textView);
        btnUpdate = (Button) findViewById(R.id.btn_update);
        mToolbar = (Toolbar) findViewById(R.id.toolbar);
        btnUpdate.setOnClickListener(this);
        mToolbar.setTitle("更新");
        setSupportActionBar(mToolbar);
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {
            case R.id.btn_update:
                checkoutUpdate();
                break;
        }

    }

    /**
     * 检查更新
     */
    private void checkoutUpdate() {
        String version = "2.3.0";
        String baseUrl = "http://news-at.zhihu.com/api/4/version/android/";
//        OkHttpClient.Builder builder=new OkHttpClient().newBuilder()
//                .connectTimeout(10, TimeUnit.SECONDS)
//                .readTimeout(10,TimeUnit.SECONDS)
//                .writeTimeout(10,TimeUnit.SECONDS);
//        int cacheSize = 10 * 1024 * 1024; // 10 MiB
//        Cache cache = new Cache(App.getContext().getCacheDir(), cacheSize);
//        builder.cache(cache);
//        builder.addInterceptor(interceptor);
//        mOkHttpClient = builder.build();

        Retrofit retrofit = new Retrofit.Builder()
                .baseUrl(baseUrl)
                .addConverterFactory(GsonConverterFactory.create())
                .build();

        UpdateService movieService = retrofit.create(UpdateService.class);
        Call<UpdateInfo> call = movieService.getUpdateInfo(version);
        call.enqueue(new Callback<UpdateInfo>() {
            @Override
            public void onResponse(Call<UpdateInfo> call, Response<UpdateInfo> response) {
                UpdateInfo info = response.body();
                Log.e(TAG, "success");
                int i = info.getStatus();
                String path = info.getUrl();
                String msg = info.getMsg();
                Log.d(TAG, msg);
                Log.d(TAG, path);
                if (i == 1) {
                    Toast.makeText(UpdateActivity.this, "存在更新", Toast.LENGTH_LONG).show();
                    Intent intent = new Intent();
                    intent.setClass(UpdateActivity.this, UpdateService.class);
                    startActivity(intent);
                } else {
                    Toast.makeText(UpdateActivity.this, "已经是最新版本", Toast.LENGTH_LONG).show();

                }
            }

            @Override
            public void onFailure(Call<UpdateInfo> call, Throwable t) {
                resultTV.setText(t.getMessage());
                Log.e(TAG, "fail");
                Log.e("throwable", t.getMessage());
            }
        });

    }
}
